Interactive rstats - UAB Biostatistics 626

Kenton Russell

October 11, 2016

Will I Ever Use This Again?

I do!

almost every day

Companies

Twitter

Google

Microsoft

Uber

…

Prominent Sightings

New York Times

FT

Economist

FiveThirtyEight

From Static to Interactive

htmlwidgets

htmlwidgets

gallery of htmlwidgets

screenshot of htmlwidgets gallery

Examples

Interactive ggplot2

plotly

ggiraph

ggiraph in print

Some plotly code

library(plotly)
ggplotly(
  ggplot(OrchardSprays, aes(x=treatment, y=decrease)) +
     geom_point(),
  width = 400, height = 300
)

Some ggiraph code

library(ggiraph)

ggp <- ggplot(OrchardSprays, aes(x=treatment, y=decrease)) +
     geom_point_interactive(aes(tooltip=decrease))
ggiraph(code = print(ggp), width=0.4)

Same in scatterD3

library(scatterD3)

scatterD3(
  x=rowpos, y=decrease, data=OrchardSprays,
  lasso=TRUE, width=400, height=300)

a litte more biostats-y

qtlcharts

library(qtlcharts)
data(geneExpr)
ipc <- iplotCorr( geneExpr$expr, geneExpr$genotype, reorder=TRUE
   ,chartOpts=list( height = 400, width = 800 )
)

qtlcharts iplotCorr

ipc

more qtlcharts

pivot tables

library(rpivotTable)

rpivotTable(OrchardSprays, height=400)

parallel coordinates

library(parcoords)
pc <- parcoords(
  OrchardSprays, brushMode="1d", reorderable=TRUE,
  rownames=FALSE, color=list(colorBy="treatment",colorScale=htmlwidgets::JS("d3.scale.category10()"))
)

networkD3

library(networkD3)
#thanks Evelina Gabasova
#http://evelinag.com/blog/2015/12-15-star-wars-social-network/#.V_zauvArI2y
network <- jsonlite::fromJSON(
  "https://cdn.rawgit.com/evelinag/StarWars-social-network/master/networks/starwars-full-interactions-allCharacters-merged.json"
)

fn <- forceNetwork(
  Links = network$links, Nodes = network$nodes,
  Source = "source", Target = "target", NodeID = "name",
  Value = "value", Nodesize = "value",
  Group = "colour", zoom = TRUE, linkDistance = 100, charge = -25
)

Star Wars force

fn

3d

plotly, rthreejs, and rgl

maps

library(leaflet)

HTML/CSS/JS in R

Manual Hard Way

cat(
  '
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>


</head>
<body style="background-color:white;">
<h1>HTML in R</h1>
</body>
</html>
  ',
  file="manual_html.html"
)
rstudioapi::viewer("manual_html.html")

htmltools

library(htmltools)

browsable(tags$h1("HTML in R"))

100 available tags

code

Rmarkdown/markdown

rmarkdown

The previous example would look like this in rmarkdown.

# HTML in R

This presentation built entirely in rmarkdown.

Back and Forth

Methods

Shiny | R <-> HTML/CSS/JS

crosstalk (experimental) | R -> HTML/CSS/JS <-> HTML/CSS/JS

?? - no agreed upon standard yet